package top.yangbuyi.controller.system;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.yangbuyi.acpect.annotaction.Log;
import top.yangbuyi.acpect.enums.BusinessType;
import top.yangbuyi.constants.Constants;
import top.yangbuyi.domain.Menu;
import top.yangbuyi.dto.MenuDto;
import top.yangbuyi.service.impl.MenuService;
import top.yangbuyi.utils.ShiroSecurityUtils;
import top.yangbuyi.vo.AjaxResult;

import javax.validation.constraints.NotNull;
import java.util.List;


/**
 * @description: 杨不易网站:www.yangbuyi.top
 * @program: yangbuyiopenHis2020
 * @ClassName: MenuController
 * @create: 2020-09-03 16:38
 * @author: yangbuyi
 * @since： JDK1.8
 * @MenuController: 菜单管理$
 **/

@RestController
@Slf4j
@RequestMapping("system/menu")
public class MenuController {

	  @Autowired
	  private MenuService menuService;

	  /**
	   * @功能描述:查询所有菜单及权限信息
	   * @Description:
	   * @Author: TeouBle
	   * @Date: 2020/9/3 16:38
	   */
	  @GetMapping("listAllMenus")
	  public AjaxResult listAllMenus(MenuDto menuDto) {
			List<Menu> menuList = this.menuService.listAllMenus(menuDto);
			return AjaxResult.success(menuList);
	  }

	  /**
	   * @功能描述:查询菜单的下拉树
	   * @Description:
	   * @Author: TeouBle
	   * @Date: 2020/9/3 16:40
	   */
	  @GetMapping("selectMenuTree")
	  public AjaxResult selectMenuTree() {
			MenuDto menuDto = new MenuDto();
			menuDto.setStatus(Constants.STATUS_TRUE);
			return AjaxResult.success(this.menuService.listAllMenus(menuDto));
	  }

	  /**
	   * @功能描述:根据ID查询菜单和权限信息
	   * @Description:
	   * @Author: TeouBle
	   * @Date: 2020/9/3 16:56
	   */
	  @GetMapping("getMenuById/{id}")
	  public AjaxResult getMenuById(@PathVariable @NotNull(message = "参数不能为空") Integer id) {
			return AjaxResult.success(this.menuService.getMenuById(id));
	  }

	  /**
	   * 根据角色ID查询当前角色ID拥有的权限菜单【只查子节点的】
	   *
	   * @param getMenuIdsByRoleId
	   * @return
	   */
	  @GetMapping("getMenuIdsByRoleId/{roleId}")
	  public AjaxResult getMenuIdsByRoleId(@NotNull(message = "参数不能为空") @PathVariable Long roleId) {
			List<Long> ids = this.menuService.getMenuIdsByRoleId(roleId);
			return AjaxResult.success(ids);
	  }

	  /**
	   * @功能描述:添加菜单和权限信息
	   * @Description:
	   * @Author: TeouBle
	   * @Date: 2020/9/3 16:53
	   */
	  @PostMapping("addMenu")
	  @Log(title = "添加菜单和权限信息", businessType = BusinessType.INSERT)
	  public AjaxResult addMenu(@Validated MenuDto menuDto) {
			try {
				  // 设置创建人
				  menuDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
				  return AjaxResult.toAjax(this.menuService.addMenu(menuDto));
			} catch (Exception e) {
				  // 返回给切面
				  return AjaxResult.ex(e.getMessage());
			}

	  }


	  /**
	   * @功能描述:修改菜单和权限信息
	   * @Description:
	   * @Author: TeouBle
	   * @Date: 2020/9/3 16:53
	   */
	  @PutMapping("updateMenu")
	  @Log(title = "修改菜单和权限信息", businessType = BusinessType.UPDATE)
	  public AjaxResult updateMenu(@Validated MenuDto menuDto) {
			try {
				  // 设置修改者
				  menuDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
				  return AjaxResult.toAjax(this.menuService.update(menuDto));
			} catch (Exception e) {
				  return AjaxResult.ex(e.getMessage());
			}
	  }


	  /**
	   * @功能描述:删除菜单数据
	   * @Description:逻辑需要判断该删除菜单是否有子菜单
	   * @Author: TeouBle/system/menu/deleteMenuById/{menuId}
	   * @Date: 2020/9/3 16:54
	   */
	  @DeleteMapping("deleteMenu/{menuid}")
	  @Log(title = "删除菜单数据", businessType = BusinessType.DELETE)
	  public AjaxResult deleteMenu(@PathVariable Long menuid) {
			try {
				  if (this.menuService.hasChilenByMenuID(menuid)) {
						// true表示有
						return AjaxResult.fail("当前删除的菜单有子节点，请先删除子节点");
				  }
				  // 没有
				  return AjaxResult.toAjax(this.menuService.deleteMenuById(menuid));
			} catch (Exception e) {
				  return AjaxResult.ex(e.getMessage());
			}
	  }


}